var ke;
var raiderManageId = $("#rowId").val();
KindEditor.ready(function(K) {
	ke = K.create('textarea[name="raiderContent"]', {
		resizeType : 0,
		urlType : 'domain',
		uploadJson : JSSYSMANAGERV1 + '/keUploadImg/uploadImg',
		items : [
		 'undo', 'redo', '|', 'selectall', 'cut', 'copy', 'paste', 'plainpaste', 'wordpaste', '|',
		 'quickformat', 'formatblock', 'fontname', 'fontsize', '|', 'forecolor',  'hilitecolor', 'bold', 
		 'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'link', 'unlink', 'image', 'emoticons', 'pagebreak', 
		 '|', 'justifyleft', 'justifycenter', 
		 'justifyright', 'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'clearhtml', '|', 'preview', 'source', 'about', 
		]
	});
});

//下拉列表显示(根据攻略大类查攻略小类)
function changeRaiderType(raiderTypeId) {
    $("#raiderTypeEle").siblings().remove();
    if (!CheckUtil.isNotNull(raiderTypeId+"")) {
        return false;
    }
	$.ajax({
		type: "post",
		url: JSSTRATEGYV1 + "/raiderType/raiderTypeSelect",
		data:{raiderTypeId:raiderTypeId,token:sessionStorage.getItem("token")},
		dataType:"json",
		async:false,
		success: function(data) {
			if (data.length<=0) {
				return;
			}
			var _html = "";
			for (var i=0; i<data.length; i++) {
                _html += "<option value= '"+data[i].value+"'>"+data[i].label+"</option>";
			}
			$("#raiderTypeEle").siblings().remove();
            $("#raiderTypeEle").after(_html);
		}
	});
}

$(function(){
    //后台登录用户token
    $("#token").val(sessionStorage.getItem("token"));
	if (CheckUtil.isNotNull(raiderManageId) && parseInt(raiderManageId) > 0) {
		$('#gameIdSelect').attr("disabled", "true");
	}
    //修改攻略类型时获取数据
    if(raiderManageId > 0){
        $.ajax({
            url: JSSTRATEGYV1 + "/raiderManage/getRaiderManageInfo",
            type: "post",
            cache: false,
            data:{"rowId":raiderManageId},
            dataType:"json",
            async:false,
            success: function(data) {
                if (data == null) {
                    return;
                }
                $("#gameId").val(data.gameId);
                $("#gameName").val(data.gameName);
                $("#gameIdSelect").val(data.gameId);
                $("#raiderTypeId").val(data.raiderTypeId);
                $("#raiderSmallId").val(data.raiderSmallId);
                $("#raiderType").val(data.raiderType);
                $("#raiderSmallName").val(data.raiderSmallName);
                $("#raiderTitle").val(data.raiderTitle);
                $("#orderByNo").val(data.orderByNo);
                $("input[name='hotFlag']").each(function(){
                    if (data.hotFlag == $(this).val()) {
                        $(this).attr("checked",true);
                    } else {
                        $(this).attr("checked",false);
                    }
                });
                $("input[name='fbFlag']").each(function(){
                    if (data.fbFlag == $(this).val()) {
                        $(this).attr("checked",true);
                    } else {
                        $(this).attr("checked",false);
                    }
                });
                $("#raiderContent").val(data.raiderContent);
                if(data.raiderImg != ""){
                    $("#newmainImgPath").attr("src", data.raiderImg);
                } else {
                    $("#newmainImgPath").attr("src", "${ctx}/images/uploadImage/noimage.gif");
                }
                $("#raiderImg").val(data.raiderImg);
                TagUtil.fillOptions('gameIdSelect');
                $("#raiderTypeId").val(data.raiderTypeId);
                changeRaiderType(data.raiderTypeId);
                $("#raiderSmallId").val(data.raiderSmallId);
            }
        });
	}
});
var err = {'1':'添加','2':'修改'};
var action = {'1': JSSTRATEGYV1 + '/raiderManage/save','2':JSSTRATEGYV1 +  '/raiderManage/update'};
function add(){
	//检查渠道游戏名称是否重复
	//var bb = checkGameName();
	var rowId = $('#rowId').val();
	var i;
	if (!CheckUtil.isNotNull(rowId) || rowId <= 0) {
		i = 1;//添加
	} else {
		i = 2;//修改
	}
	var gameId = $("#gameIdSelect").val();
	if (!CheckUtil.isNotNull(gameId)) {
		layer.msg("请选择游戏!");
		return;
	}
	var raiderTypeId = $("#raiderTypeId").val();
	if (!CheckUtil.isNotNull(raiderTypeId)) {
		layer.msg("请选择攻略类型!");
		return;
	}
    var raiderSmallId = $("#raiderSmallId").val();
    if (!CheckUtil.isNotNull(raiderSmallId)) {
        layer.msg("请选择攻略小类!");
        return;
    }
    var orderByNo = $('#orderByNo').val();
    if (!CheckUtil.isNotNull(orderByNo) || !CheckUtil.isInt(orderByNo, "+") || orderByNo == 0) {
        layer.msg("请输入正确的排序编号!");
        return;
    }
	var raiderTitle = $("#raiderTitle").val();
	if (!CheckUtil.isNotNull(raiderTitle)) {
		layer.msg("请输入攻略标题!");
		return;
	}
	if (CheckUtil.isNotNull(raiderTitle) && raiderTitle.length > 40) {
		layer.msg("攻略标题不能超过40个字!");
		return;
	}
    var hotFlag =$('input:radio[name="hotFlag"]:checked').val();
    if (!CheckUtil.isNotNull(hotFlag)) {
        layer.msg("请选择是否热门!");
        return;
    }
	var fbFlag =$('input:radio[name="fbFlag"]:checked').val();
	if (!CheckUtil.isNotNull(fbFlag)) {
		layer.msg("请选择是否发布!");
		return;
	}
	var imgPath = $("#imgPath").val();//图片
    var raiderImg = $("#raiderImg").val();
	if (!CheckUtil.isNotNull(imgPath) && !CheckUtil.isNotNull(raiderImg)) {
		layer.msg("请选择所要上传的图片!");
		return;
	}
	if (CheckUtil.isNotNull(imgPath) && !CheckUtil.isImg(imgPath)) {
		layer.msg("上传文件只能为图片!");
		return ;
	}
	//富编辑框内容
	ke.sync();
	if (!CheckUtil.isNotNull(ke.text())) {
		layer.msg("请输入攻略内容!");
		return;
	}
    if ($("#isReCommit").val() == 0) {
        $("#isReCommit").val(1);
    }
    //loading层
    var index = layer.load(0, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
    if (!CheckUtil.isNotNull(rowId) || rowId == 0) {
		$("#gameId").val(gameId);
        $("#gameName").val($("#gameIdSelect option:selected").text());
    }
    $("#raiderType").val($("#raiderTypeId option:selected").text());
    $("#raiderSmallName").val($("#raiderSmallId option:selected").text());
    
    $.ajax({
        url: action[i],
        type: "post",
        cache: false,
        data: new FormData($("#saveAndUpdateFrom")[0]),
        dataType:"json",
        processData: false,
        contentType: false,
        async:false,
        success: function(data) {
            layer.close(index);
            if(data.code == 0){
                parent.layer.msg("添加成功！");
            } else if(data.code == 1){
                parent.layer.msg("修改成功！");
            } else if(data.code == "-2"){
                parent.window.location.href="${sysManagerV1}/";
            } else {
                parent.layer.msg(data.message);
            }
            if(data.code >= 0){
                parent.findData();
                parent.layer.close(parent.tableIndex);
            }
        }
    });
}